package com.gdzn.signal.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gdzn.common.core.domain.R;
import com.gdzn.common.core.utils.JsonUtil;
import com.gdzn.signal.mongo.document.*;
import com.gdzn.signal.service.impl.SignalServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(value = "/signal", tags = "保存设备上报信号")
@Tag(name = "保存设备上报信号")
@RestController
@RequestMapping("/signal" )
public class SignalTestController {

    @Autowired
    private SignalServiceImpl signalServiceImpl;

    /**
     * 保存上报信号
     * @param signal
     * @return
     */
    @PostMapping
    public R insert(@RequestBody MgSignal signal) {
        signalServiceImpl.saveSignal(signal);
        return R.ok(signal);
    }

    @PostMapping("/alarm")
    public R alarm(@RequestBody MgSignalAlarm signal) {
        signalServiceImpl.saveSignal(signal);
        return R.ok(signal);
    }

    @PostMapping("/jsonStr")
    public R add2(@RequestBody String jsonStr) {
        Map paramMap = JsonUtil.toMap(jsonStr);
        if("110".equals(paramMap.get("msgType").toString()) || "111".equals(paramMap.get("msgType").toString())){
            MgSignalLogin signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalLogin(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("310".equals(paramMap.get("msgType").toString()) || "311".equals(paramMap.get("msgType").toString())){

            MgSignalAttribute signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAttribute(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("410".equals(paramMap.get("msgType").toString()) || "411".equals(paramMap.get("msgType").toString())){
            MgSignalAlarm signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAlarm(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("500".equals(paramMap.get("msgType").toString()) || "501".equals(paramMap.get("msgType").toString())){
            MgSignalCmdControl signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdControl(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("210".equals(paramMap.get("msgType").toString()) || "211".equals(paramMap.get("msgType").toString())){
            MgSignalCmdQconfig signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdQconfig(),false);
            signalServiceImpl.saveSignal(signal);
        }
        return R.ok();
    }

    @PostMapping("/add")
    public R add(@RequestBody Map<String,Object> paramMap) {
        if("110".equals(paramMap.get("msgType").toString()) || "111".equals(paramMap.get("msgType").toString())){
            MgSignalLogin signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalLogin(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("310".equals(paramMap.get("msgType").toString()) || "311".equals(paramMap.get("msgType").toString())){

            MgSignalAttribute signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAttribute(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("410".equals(paramMap.get("msgType").toString()) || "411".equals(paramMap.get("msgType").toString())){
            MgSignalAlarm signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAlarm(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("500".equals(paramMap.get("msgType").toString()) || "501".equals(paramMap.get("msgType").toString())){
            MgSignalCmdControl signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdControl(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("210".equals(paramMap.get("msgType").toString()) || "211".equals(paramMap.get("msgType").toString())){
            MgSignalCmdQconfig signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdQconfig(),false);
            signalServiceImpl.saveSignal(signal);
        }
        return R.ok();
    }
}
